local PANEL = {}

function PANEL:Init()

	self:SetTitle("");
	self:SetDraggable(false);
	self:ShowCloseButton(false);
	
	self.TxtCanvas = vgui.Create( "DPanelList", self );
	self.TxtCanvas.Paint = function( ) return false end
	
	self.TextEntry = vgui.Create( "DTextEntry", self );
	self.TextEntry:SetVisible( true );
	self.TextEntry:SetCursorColor( GPM.Colors.TextEntryCursor );
	self.TextEntry:SetTextColor( GPM.Colors.TextEntryText );
	self.TextEntry:SetDrawBackground( false );
	self.TextEntry:SetDrawBorder( false );
	self.TextEntry.OnEnter = function( )
		if ( self.ChatEntered ) then
			self:ChatEntered( self.TextEntry:GetValue( ) );
		end
		self.TextEntry:SetValue( "" );
		self.TextEntry:SetText( "" );
		self.TextEntry:RequestFocus();
	end
	self.AnimIn = Derma_Anim("Open", self, self.OpenAnim);
	self.AnimOut = Derma_Anim("Close", self, self.CloseAnim);
end

function PANEL:Open(targettall)
	self.AnimIn:Stop();
	self.AnimOut:Stop();
	self.AnimIn:Start(0.2, {TargetTall = targettall});
	self:MakePopup();
end

function PANEL:Close()
	self.AnimIn:Stop();
	self.AnimOut:Stop();
	self.AnimOut:Start(0.4, {TargetTall = self.Tall});
end

function PANEL:OpenAnim(anim, delta, data)
	if anim.Finished then
		self.Tall = data.TargetTall;
		self:SetTall(self.Tall);
		return;
	end
	self.Tall = data.TargetTall*delta
	self:SetTall(self.Tall);
end

function PANEL:CloseAnim(anim, delta, data)
	if anim.Finished then
		self:SetVisible(false);
		self:Remove();
		return;
	end
	self.Tall = self.Tall - data.TargetTall*delta
	self:SetTall(self.Tall);
end

function PANEL:Paint()
	surface.SetDrawColor( GPM.Colors.ChatBG );
	surface.DrawRect( 0, 0, self:GetWide(), self:GetTall( ) );
	local x, y, w, h = self.TextEntry:GetBounds( );
	surface.SetDrawColor( GPM.Colors.TextEntryBG );
	surface.DrawRect( x, y, w, h );
	return false;
end

function PANEL:Think()
	self.TextEntry:SetCursorColor( GPM.Colors.TextEntryCursor );
	self.TextEntry:SetTextColor( GPM.Colors.TextEntryText );
	self.AnimIn:Run();
	self.AnimOut:Run();
end

function PANEL:PerformLayout()
	self.AnimIn:Run();
	self.AnimOut:Run();
	self.TxtCanvas:SetPos( 5, 5 );
	self.TxtCanvas:SetSize( self:GetWide( ) - 10, self:GetTall( ) - 34 );
	self.TxtCanvas:SetSpacing( 4 );
	self.TxtCanvas:EnableHorizontal( false );
	self.TxtCanvas:SetVisible( true );
	self.TxtCanvas:EnableVerticalScrollbar( true );
	
	self.TextEntry:SetSize( self:GetWide( ) - 10, 20 );
	self.TextEntry:SetPos( 5, self:GetTall() - 25 );
end

function PANEL:SetChat(chat)
	timer.Simple(0.08, function()
		for _, t in ipairs(chat) do
			self:AddText(t.Text, t.Player);
		end
	end);
end

function PANEL:AddText(text, ply)
	surface.SetFont( "Default" );
	local label = vgui.Create( "DLabel" );
	label:SetFont( "Default" );
	local c;
	if ply == LocalPlayer() then
		c = GPM.Colors.Local;
	elseif GPM.IsSteamFriend(ply) then
		c = GPM.Colors.FriendText;
	else
		c = GPM.Colors.Text;
	end
	label:SetTextColor(c);
	label:SetText(text);
	label:SetAutoStretchVertical(true);
	label:SetWide(self.TxtCanvas:GetWide() - 16);
	label:SetWrap(true);
	self.TxtCanvas:AddItem(label);
	if self.TxtCanvas.VBar then
		timer.Simple( 0.01, self.TxtCanvas.VBar.SetScroll, self.TxtCanvas.VBar, 9e9 );
	end
end

derma.DefineControl( "GPMFrameBody", "Private Message Window made by MakeR", PANEL, "DFrame" );